"""
    读取文件
"""
import configparser
import json
import os.path
import yaml


def getProjectRoot():
    # print("开始获取项目根目录")
    current_file_path = os.path.realpath(__file__)
    # print("获取当前文件", current_file_path)
    current_dir_path = os.path.dirname(current_file_path)
    # print("获取当前文件所在的目录", current_file_path)
    current_project_path = os.path.dirname(current_dir_path)
    # print("获取项目根目录:", current_file_path)
    return current_project_path


def read_config(option, section='env'):
    print("读取test.ini文件")
    print(getProjectRoot())
    ini_file_path = getProjectRoot() + r"\test.ini"
    config = configparser.ConfigParser()
    config.read(ini_file_path)
    return config.get(section, option)


def read_yaml(file_name):
    #   拼接yaml数据文件的路径
    file_path = getProjectRoot() + "\\test_data\\" + file_name
    #   读取yaml数据文件的内容
    with open(file_path, mode='r', encoding='utf-8') as f:
        c = f.read()
    #   文件内容按照yaml格式加载到python中
    return yaml.load(c, Loader=yaml.FullLoader)


if __name__ == '__main__':
    print(read_config("base_url"))
    print(read_config("test", 'other'))
    print(read_config("db_info"))
    # read_config("db_info")["host"]
    db_info = json.loads(read_config("db_info"))
    print(db_info["host"])
    y = read_yaml("register_fail_data.yaml")
    print(y)
    for y1 in y:
        print(y1["data"])
